<?php

header ("Content-type: image/jpeg");

$photo_id = $id;
$photo = $photo_source->getPhoto($photo_id);
$path = $photo->getPath();
$cache = $site->getPhotoCache();

$w = $_GET['w'];
$h = $_GET['h'];
$dims = new Dims($h, $w);

if ($cache->works()) {
    $stats = stat($path);
    $data = $cache->getData($source_id, $photo_id, $dims, $stats['mtime']);
    if ($data !== FALSE) {
        echo $data;
        die();
    }
}

if ($photo instanceof UriMovie_I) {
    $movie = new MovieViewer($photo);
    $img = $movie->getSnapshotImage($dims);
    $orig_w = imagesx($img);
    $orig_h = imagesy($img);
} else {

    //1 = GIF, 2 = JPG, 3 = PNG, 4 = SWF, 5 = PSD, 6 = BMP, 7 = TIFF(orden de bytes intel), 8 = TIFF(orden de bytes motorola), 9 = JPC, 10 = JP2, 11 = JPX, 12 = JB2, 13 = SWC, 14 = IFF, 15 = WBMP, 16 = XBM. 
    
    $img_stats = getimagesize($path);
    $orig_w = $img_stats[0];
    $orig_h = $img_stats[1];
    
    switch ($img_stats[2]) {
        case 1:
            $img = imagecreatefromgif($path);
            break;
    
        case 2:
            $img = imagecreatefromjpeg($path);
            break;
    
        case 3:
            $img = imagecreatefrompng($path);
            break;
    }
}

$resized_img = imagecreatetruecolor($w, $h);

imagecopyresampled($resized_img, $img, 0, 0, 0, 0, $w, $h, $orig_w, $orig_h);
imagedestroy($img);
imagejpeg($resized_img);
if ($cache->works()) {
    $cache->saveImage($source_id, $photo_id, $resized_img, $dims);
}
imagedestroy($resized_img);

